package xyz.zhzby.cyzx.ucenter.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import xyz.zhzby.cyzx.security.util.CyzxSecurityContextHolder;
import xyz.zhzby.cyzx.servicebase.domain.DataLogInfo;
import xyz.zhzby.cyzx.servicebase.domain.RestResponse;
import xyz.zhzby.cyzx.servicebase.utils.MDUtil;
import xyz.zhzby.cyzx.servicebase.utils.StringUtils;
import xyz.zhzby.cyzx.ucenter.convert.UserConvert;
import xyz.zhzby.cyzx.ucenter.dto.request.PasswordRequest;
import xyz.zhzby.cyzx.ucenter.dto.response.UserResponse;
import xyz.zhzby.cyzx.ucenter.entity.User;
import xyz.zhzby.cyzx.ucenter.mapper.UserMapper;
import xyz.zhzby.cyzx.ucenter.service.IInterestService;
import xyz.zhzby.cyzx.ucenter.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhzby.cyzx.ucenter.util.RedisUtil;

/**
 * <p>
 * 用户类 服务实现类
 * </p>
 *
 * @author zby
 * @since 2021-12-16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private IInterestService interestService;

    @Override
    public RestResponse<UserResponse> getCurrentUserInfo() {
        xyz.zhzby.cyzx.security.entity.User subject = CyzxSecurityContextHolder.getSubject();
        UserResponse userResponse = UserConvert.INSTANCE.USER_RESPONSE(subject);
        userResponse.setInterests(interestService.getInterestsByUserId(subject.getId()));
        return RestResponse.success(userResponse);
    }

    @Override
    public RestResponse<String> updateInfo(User user) {
        CyzxSecurityContextHolder.getSubject().fillUpdateInfo(user, DataLogInfo.USER_FIELD_ACTUAL_NAME);
        RedisUtil.updateUserInfo(UserConvert.INSTANCE.USER_USER(user));
        // 下面三个属性都是单独修改，需要在此排除
        user.setAvatar(null);
        user.setEmail(null);
        user.setPhone(null);
        baseMapper.updateById(user);
        return RestResponse.success("更新成功");
    }

    @Override
    public RestResponse<String> updateEmail(String email, String code) {
        xyz.zhzby.cyzx.security.entity.User subject = CyzxSecurityContextHolder.getSubject();
        if (email.equals(subject.getEmail())) {
            return RestResponse.fail("新旧邮箱不能一样");
        }
        String verificationCode = RedisUtil.getVerificationCode("code:email:" + subject.getUsername());
        if (code.equals(verificationCode)) {
            User user = new User();
            user.setId(subject.getId());
            user.setEmail(email);
            subject.fillUpdateInfo(user, DataLogInfo.USER_FIELD_ACTUAL_NAME);
            baseMapper.updateById(user);
            subject.setEmail(email);
            RedisUtil.updateUserInfo(subject);
            return RestResponse.success("更新成功");
        }
        return RestResponse.fail("验证码错误");
    }

    @Override
    public RestResponse<String> updatePhone(String phone, String code) {
        xyz.zhzby.cyzx.security.entity.User subject = CyzxSecurityContextHolder.getSubject();
        if (phone.equals(subject.getPhone())) {
            return RestResponse.fail("新旧手机号不能一样");
        }
        String verificationCode = RedisUtil.getVerificationCode("code:phone:" + subject.getUsername());
        if (code.equals(verificationCode)) {
            User user = new User();
            user.setId(subject.getId());
            user.setPhone(phone);
            subject.fillUpdateInfo(user, DataLogInfo.USER_FIELD_ACTUAL_NAME);
            baseMapper.updateById(user);
            subject.setPhone(phone);
            RedisUtil.updateUserInfo(subject);
            return RestResponse.success("更新成功");
        }
        return RestResponse.fail("验证码错误");
    }

    @Override
    public RestResponse<String> updateAvatar(String avatar) {
        xyz.zhzby.cyzx.security.entity.User subject = CyzxSecurityContextHolder.getSubject();
        if (StringUtils.isNotNullOrBlank(avatar)) {
            User user = new User();
            user.setId(subject.getId());
            user.setAvatar(avatar);
            subject.fillUpdateInfo(user, DataLogInfo.USER_FIELD_ACTUAL_NAME);
            baseMapper.updateById(user);
            subject.setAvatar(avatar);
            RedisUtil.updateUserInfo(subject);
            return RestResponse.success("更新成功");
        }
        return RestResponse.fail("更新失败，请重试");
    }

    @Override
    public RestResponse<String> updatePassword(PasswordRequest passwordRequest) {
        xyz.zhzby.cyzx.security.entity.User subject = CyzxSecurityContextHolder.getSubject();
        if (!MDUtil.compare(passwordRequest.getOldPassword(), subject.getPassword())) {
            return RestResponse.fail("当前密码输入错误");
        }
        if (MDUtil.compare(passwordRequest.getNewPassword(), subject.getPassword())) {
            return RestResponse.fail("新密码与当前密码相同");
        }
        User user = new User();
        user.setId(subject.getId());
        user.setPassword(MDUtil.encryption(passwordRequest.getNewPassword()));
        subject.fillUpdateInfo(user, DataLogInfo.USER_FIELD_ACTUAL_NAME);
        baseMapper.updateById(user);
        subject.setPassword(user.getPassword());
        RedisUtil.updateUserInfo(subject);
        return RestResponse.success("更新成功");
    }
}
